﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using HienThi;

namespace DAO
{
    public class ThongKeDDHDAO
    {
        public int LaySoLuong_ChiTietDDH(string maddh, string masp)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            string sqlStr = "select ctddh.SoLuong from ChiTietDonDatHang ctddh, SanPham sp where ctddh.MaDDH = @maddh and sp.MaSP = @masp and ctddh.MaSP = sp.ID";
            SqlCommand cmd = new SqlCommand(sqlStr, conn);
            SqlParameter pmaddh = new SqlParameter("@maddh", maddh);
            SqlParameter pmasp = new SqlParameter("@masp", masp);
            cmd.Parameters.Add(pmaddh);
            cmd.Parameters.Add(pmasp);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                return int.Parse(dr["SoLuong"].ToString());
            }
            else
                return 0;

        }
        public List<string> LayMaDDH(int thang, int nam)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            string sqlStr = "select MaDDH from DonDatHang where Month(NgayDat) =@thang and YEAR(NgayDat)=@nam";
            SqlCommand cmd = new SqlCommand(sqlStr, conn);
            SqlParameter pthang = new SqlParameter("@thang", thang);
            SqlParameter pnam = new SqlParameter("@nam", nam);
            cmd.Parameters.Add(pthang);
            cmd.Parameters.Add(pnam);
            SqlDataReader dr = cmd.ExecuteReader();
            List<string> kq = new List<string>();
            while (dr.Read())
            {
                string temp = dr["MaDDH"].ToString();
                kq.Add(temp);
            }
            return kq;
        }
        public ThongKeDDHHienThi LayBangThongKeDDHHienThi(string maddh, int stt)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            string sqlStr = "select MaDDH, NgayDat,TongTien,TinhTrang from DonDatHang where MaDDH = @maddh";
            SqlCommand cmd = new SqlCommand(sqlStr, conn);
            SqlParameter pmaddh = new SqlParameter("@maddh", maddh);
            cmd.Parameters.Add(pmaddh);
            SqlDataReader dr = cmd.ExecuteReader();
            ThongKeDDHHienThi kq = new ThongKeDDHHienThi();
            while (dr.Read())
            {
                kq.STT = stt;
                kq.MaDDH = dr["MaDDH"].ToString();
                kq.NgayDat = DateTime.Parse(dr["NgayDat"].ToString());
                kq.TongTien = float.Parse(dr["TongTien"].ToString());
                kq.TinhTrang = int.Parse(dr["TinhTrang"].ToString());

            }
            kq.Ch2oz = LaySoLuong_ChiTietDDH(maddh, "74187");
            kq.Ch25oz = LaySoLuong_ChiTietDDH(maddh, "74888");
            kq.Ch32oz = LaySoLuong_ChiTietDDH(maddh, "74191");
            kq.Argi9 = LaySoLuong_ChiTietDDH(maddh, "74162");
            kq.Argi9Hop = LaySoLuong_ChiTietDDH(maddh, "74167");
            kq.Mistica = LaySoLuong_ChiTietDDH(maddh, "74005");
            kq.Kits = LaySoLuong_ChiTietDDH(maddh, "77247");
            return kq;
        }
    }
}
